﻿/*
 * ===================================================
 * Author     : <ClueeZhuo>
 * Create Time: <2018/5/13 3:31:20>
 * Description: <This is a Description of This File.>
 * ===================================================
 */




using Microsoft.AspNetCore.Mvc;
using System;

namespace helloworld.IOC
{
    public partial class ClueeMVCEngine : MVCEngine
    {

        #region ===TemplateMethod==
        //资产端资产信息业务逻辑单实例
        private static Controller controller = null;
        //多线程安全锁
        private static readonly object syslock = new object();

        /// <summary>
        /// 重写控制器激活:使用单利提高性能
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override Controller OnActivateControler(Request request)
        {
            if (controller == null)
            {
                lock (syslock)
                {
                    if (controller == null)
                    {
                        controller = base.OnActivateControler(request);
                    }
                }
            }
            return controller;
        }
        #endregion
    }

    public partial class ClueeMVCEngine : MVCEngine
    {
        #region ===FactoryMethod===

        public class SingletonControllerActivator : ControllerActivator
        {
            public override Controller ActivateController(Request request)
            {
                if (controller == null)
                {
                    lock (syslock)
                    {
                        if (controller == null)
                        {
                            controller = base.ActivateController(request);
                        }
                    }
                }
                return controller;
            }
        }


        public override ControllerActivator GetControllerActivator()
        {
            return new SingletonControllerActivator();
        }

        #endregion
    }

    public partial class ClueeMVCEngine : MVCEngine
    {
        #region ===AbstractFactory===

        public class SingletonActivator : AbstractFactory
        {
            public override ControllerActivator GetActivator()
            {
                return new SingletonControllerActivator();
            }
        }

        public void Run()
        {
            new MVCEngine(new SingletonActivator()).StartByAbstractFactory(new Uri("http://localhost/mvcapp"));
        }

        #endregion
    }

    public partial class ClueeMVCEngine : MVCEngine
    {
        #region ===由外部容器提供服务对象===
        public void Run2()
        {
            Cat cat = new Cat();
            MVCEngine engine = new MVCEngine(cat);
            Uri address = new Uri("http://localhost/mvcapp");
            engine.Start(address);
        }

        #endregion
    }
}

